package com.lenovo.nova.util.collection;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import com.lenovo.nova.util.debug.mylog;


public class Collections {

	private static final String TAG = Collections.class.getSimpleName();

	public <E> Collection<E> intersection(Collection<E> ...collections ){
		int minSizeIndexCollection = findMinSizeForCollection(collections);
		//找出size最小的集合
		Collection<E> minSizeCollection = collections[minSizeIndexCollection];
		
		//遍历最小的集合
		for (E e : minSizeCollection) {
			
			for (int i = 0; i < collections.length; i++) {
				///遍历所有的集合
				Collection<E> collection = collections[i];
				if(!collection.contains(e)){
					minSizeCollection.remove(e);
					break;
				}
			}
			
		}
		return minSizeCollection;
	}
	
	public static <E> String showCollection(Collection<E> collection,OnShowCollectionHelper<E> helper){
		Iterator<E> it = collection.iterator();
		StringBuffer sb = new StringBuffer();
		while(it.hasNext()){
			E e = it.next();
			sb.append(e);
			sb.append("\n");
			if(helper != null){
				helper.onItem(e);
			}	
		}
		boolean internalLog = true;
		if(helper != null){
			internalLog = !helper.onCloseInternalPrint();
		}
		
		if(internalLog){
			System.out.println("collection.size is " + collection.size());
			System.out.println(sb.toString());
		}
		
		return sb.toString();
	}
	
	/**
	 * 找出集合数组中，size最小的集合的索引
	 * @param collections
	 * @return
	 */
	public <E>int findMinSizeForCollection(Collection<E> ...collections){
		if(collections != null && collections.length > 0){
			int minIndex = 0;
			int minSizeForCollection = collections[0].size();
			for (int i = 0 ; i < collections.length; i++) {
				Collection<E> collection = collections[i];
				if(minSizeForCollection > collection.size()){
					minSizeForCollection = collection.size();
					minIndex = i;
				}
			}
			return minIndex;
		}
		
		return -1;
	}
	
	/**
	 * 找出集合数组中，size最大的集合的索引
	 * @param collections
	 * @return
	 */
	public <E>int findMaxSizeForCollection(Collection<E> ...collections){
		if(collections != null && collections.length > 0){
			int maxIndex = 0;
			int maxSizeForCollection = collections[0].size();
			for (int i = 0 ; i < collections.length; i++) {
				Collection<E> collection = collections[i];
				if(maxSizeForCollection < collection.size()){
					maxSizeForCollection = collection.size();
					maxIndex = i;
				}
			}
			return maxIndex;
		}
		
		return -1;
	}
	
	
	public static String mapToUrl(Map<String, String> params,String paramsEncoding){
		if (params == null) {
			return null;
		}
		StringBuilder encodedParams = new StringBuilder();
		for (Map.Entry<String, String> entry : params.entrySet()) {
			encodedParams.append(encoding(entry.getKey(), paramsEncoding));
			encodedParams.append('=');
			encodedParams.append(encoding(entry.getValue(), paramsEncoding));
			encodedParams.append('&');
		}
		String stringParam = encodedParams.toString();
		return stringParam.substring(0, stringParam.length() - 1);
	}
	
	private static String encoding(String content, String encoding){
		if(encoding != null){
			mylog.i(TAG, "content " + content + "  encoding " + encoding);
			try {
				if(content == null){
					return null;
				}
				return URLEncoder.encode(content,encoding);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		
		return content;
	}
	
	public static interface OnShowCollectionHelper<E>{
		public void onItem(E e);
		public boolean onCloseInternalPrint();
	}
}
